<template>
    <div id="member">
        <el-col :span="24" class="action-bar">
            <el-button type="primary" size="medium" @click="signatureVisible=true">添加</el-button>
        </el-col>
        

        <!-- 列表 -->
        <el-table
            :data="tableData"
            style="width: 100%">
            <el-table-column
                prop="name"
                label="名称"
                width="200"
                >
            </el-table-column>
            <el-table-column
                prop="created_at"
                label="创建时间"
                width="200"
                >
            </el-table-column>
            <el-table-column
                label="状态"
                width="200">
                <template slot-scope="scope">
                        <el-switch
                        active-value="1"
                        inactive-value="2"
                        @change="switchShowStatus($event,scope)"
                        v-model="scope.row.is_show"
                        >
                        </el-switch>
                </template>
            </el-table-column>
            
            <el-table-column label="操作" width='200'>
                <template slot-scope="scope">
                    <el-button size="mini" type='primary' @click="editorClassify(scope)">编辑</el-button>
                    <el-button size="mini" type='danger' @click="delNewsClassify(scope.row.id)">删除</el-button>
                </template>
            </el-table-column>
            
        </el-table>

		<el-dialog title="资讯类型" :visible.sync="signatureVisible" @close="initData" width="30%">
            <el-form>
                <el-form-item label="类别名称">
                    <el-input palceholder="类别名称" v-model="member.content"></el-input>
                </el-form-item>
                <el-form-item label="开启">
                    <el-switch
                        active-value="1"
                        inactive-value="2"
                        v-model="member.status"
                        >
                        </el-switch>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submit" >提交</el-button>
			</div>
		</el-dialog>
    </div>
</template>
<script>
export default {
    name:'member',
    data() {
        return {
            signatureVisible:false,//编辑弹窗
            id:'',//选中的id
            tableData: [],
            page:1,
            totalPage:0,
            member:{
                content:'',
                status:'1'
            }
        }
    },
    methods: {      
        pageChange(e){
			//翻页
			this.page=e;
			this.getNewsClassifyList();
        },
        async getNewsClassifyList(){
            let res=await this.$api.getNewsClassifyList();
            if(res.data.level=='success'){
                this.tableData=res.data.data;
                // this.totalPage=res.data.page_info.total;
            }
        },
        editorClassify(scope){
            this.id=scope.row.id;
            this.member.content=scope.row.name;
            this.member.status=scope.row.is_show;
            this.signatureVisible=true
        },
        async delNewsClassify(id){
            let judge=null;
            let tit='删除该资讯类别?';
            try {
              judge=  await this.$confirm(tit, '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
                })
            } catch (error) {
                judge='cancel'
            }
            if(judge=='confirm'){
                this.delNewsClassifyHandle(id)
            }
            console.log(judge)
        },
        async delNewsClassifyHandle(id){
            let res =await this.$api.delNewsClassify({id:id})
            if(res.data.level=='success'){
                this.$message({
                    type:'success',
                    message:res.data.message
                })
                this.signatureVisible = false
                this.getNewsClassifyList()
            }else{
                this.$message({
                    type:'error',
                    message:res.data.message
                })
            }
        },
        async switchShowStatus($event,scope){
            let params={
                name:scope.row.name,
                id:scope.row.id,
                is_show:$event,
            }
            let res=await this.$api.setNewsClassify(params)
            if(res.data.level=='success'){
                this.$message({
                    type:'success',
                    message:res.data.message
                })
                this.signatureVisible = false
                
            }else{
                this.$message({
                    type:'error',
                    message:res.data.message
                })
                let is_show=$event=='1'?'2':'1'
                this.$set(this.tableData,scope.$index,{...scope.row,...{is_show:is_show}})
                
            }
        },
        async submit(){
            let params={
                name:this.member.content,
                id:this.id,
                is_show:this.member.status
            }
            let res=await this.$api.setNewsClassify(params)
            if(res.data.level=='success'){
                this.$message({
                    type:'success',
                    message:res.data.message
                })
                this.signatureVisible = false
                this.getNewsClassifyList()
            }else{
                this.$message({
                    type:'error',
                    message:res.data.message
                })
                
            }
        },

        initData(){

        }

    },
    mounted() {
        this.getNewsClassifyList()
    },
}
</script>
<style lang="scss" scoped>
.screen{
          margin-top: -10px !important;
      }
    .action-bar{
			background-color: #f2f2f2;
			height: 60px;
			margin: 10px 0px;
			box-sizing: border-box;
			display: flex;
			align-items: center;
			padding-left: 20px;
			.action-bar-input{
				width: 250px;
				margin-right: 14px;
      }
      .task-form-text{
				color: #666666;
			}
		}
		.el-table-style{
			width: 100%;
			border: 1px solid #f2f2f2;
		}
		.footer-bar{
			display: flex;
			justify-content: space-between;
			align-items: center;
			background: #f2f2f2;
			padding: 8px;
			margin: 10px 0px;
			box-sizing: border-box;
		}
		.dialog-footer{
			text-align: center;
		}
		.executorList{
			display: flex;
			flex-wrap: wrap;
			div{
				margin:10px;
				border:1px solid #eee;
				border-radius: 5px;
				padding:5px 10px;
			}
		}
</style>